import type { CsCascaderProps } from 'csui'

type UseCsCascader<
  D extends Record<string, any>,
  P extends keyof Readonly<D>
> = UseFormItemProps<CsCascaderProps, D, P>

/**
 * el-input
 * @param prop item.prop
 * @param label item.label
 * @param props item的其他属性
 * @returns form-item-data
 */
export function useCascader<
  T extends Record<string, any>,
  P extends keyof T
>(prop: P, label: string, props: UseCsCascader<T, P> = {}): FormItemProps<T, P> {
  let arrProp = prop as string
  if (!props.change) {
    props.change = (value, items, formData) => {
      formData[prop] =
        value?.length && Array.isArray(value[0]) ?
          value?.map((item: any[]) => item.at(-1)) :
          value.at(-1) || undefined
    }
    arrProp = `${String(prop)}Arr`
  }
  return {
    label,
    prop: arrProp,
    component: 'cs-cascader',
    ...props
  }
}
